package cn.watson.org.service.impl;

import cn.watson.basic.service.impl.BaseServiceImpl;
import cn.watson.org.domain.Department;
import cn.watson.org.mapper.DepartmentMapper;
import cn.watson.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService{

    @Autowired
    private DepartmentMapper departmentMapper;


    /*
    @Override
    public PageList<Department> queryData(DepartmentQuery departmentQuery) {
        // 1.查询分页总条数
        Integer total = departmentMapper.queryCount(departmentQuery);

        // 2.查询当前页数据
        List<Department> rows = departmentMapper.queryData(departmentQuery);
        return new PageList<>(total,rows);
    }
    */

    /*
    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }*/

    /*
    @Override
    public Department loadById(Long id) {
        return departmentMapper.loadById(id);
    }*/

    /*
    @Override
    public void remove(Long id) {
        departmentMapper.remove(id);
    }*/

    @Override
    @Transactional
    public void update(Department department) {
        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent().getId() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent().getId() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


   /* 批量删除
    @Override
    public void patchDel(List<Long> ids) {
        departmentMapper.patchDel(ids);
    }*/


    /**
    * @Title: tree
    * @Description: 无限极树，查询上级部门
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/26 9:01
    * @Parameters: []
    * @Return java.util.List<cn.watson.org.domain.Department>
    */
    @Override
    public List<Department> tree() {
        // 1.查询出部门的所有数据
        List<Department> depts = departmentMapper.loadAll();
        // 2. 创建一个空集合，装完成的组装的部门树数据
        List<Department> deptstree = new ArrayList<>();

        // 2.1将集合数据转换成mpa，消除时间复杂度问题【优化1】
        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department department:depts){
            deptMap.put(department.getId(),department);
        }

        // 3.遍历获取到的部门所有数据
        for (Department deptTmp:depts){
            // 4.只要上级部门为null 那就时一级部门，放到集合中
            if (deptTmp.getParentId() == null){
                deptstree.add(deptTmp);
            } else {

                Department department = deptMap.get(deptTmp.getParentId());
                if (department != null){
                    department.getChildren().add(deptTmp);
                }

            }
        }

        return deptstree;
    }
}
